<?php

namespace app\web\controller;
use app\web\model\NewModel;
use app\web\model\NewTypeModel;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 文章管理
 */
class NewController extends StoreBaseController
{
 	protected $model;
	protected $type_model;
	
    protected function _initialize() {
        parent::_initialize();
		$this->model = new NewModel();
        $this->type_model = new NewTypeModel();
		$store_id = session('storeInfo')['id'];
		$this->assign('newtype_list',$this->type_model->where('sid',$store_id)->select());
    }
	
    /**
     * 文章
     * @author [longx] 
     */
    public function index( $page = 1,$typeid=''){
    	$store_id = session('storeInfo')['id'];
		$param = input('post.');
		if($typeid != 0){
			$param['typeid'] = $typeid;
		}		
		$param['sid'] = $store_id;
        $list = $this->model->where($param)->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }
	
	/**
     * 文章类型
     * @author [longx] 
     */
    public function indextype( $page = 1){
    	$store_id = session('storeInfo')['id'];
        $list = $this->type_model->where('sid',$store_id)->paginate(10);
        return $this->fetch('indextype', ['list' => $list]);
    }
	


    /**
     * 
     *添加
     * @author [longx] 
     */
    public function add()
    {
        if(request()->isAjax()){
        	$param=input('post.');
			$param['addtime'] = now_time();
			$param['sid'] = session('storeInfo')['id'];
			$param['typeid'] = $param['newtype'];
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('New/index'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }
	
	 /**
     * 
     *添加
     * @author [longx] 
     */
    public function addtype()
    {
        if(request()->isAjax()){
        	$param=input('post.');
			$param['addtime'] = now_time();
			$param['sid'] = session('storeInfo')['id'];
			if ($this->type_model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('New/indextype'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }


    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [lx]
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');  
			$param['typeid'] = $param['newtype'];
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('New/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }
	
	 /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [lx]
     */
    public function edittype($id)
    {
        if(request()->isAjax()){
            $param = input('post.');     
         	if ($this->type_model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('New/indextype'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->type_model->find($id));
        return $this->fetch();
    }
	



    /**
     * [del删除]
     * @author [longx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
	
	 /**
     * [del删除]
     * @author [longx] 
     */
    public function deletetype()
    {
        $id = input('param.id');
        if ($this->type_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}
